# -*-coding:utf-8-*-

class Property:

    def __init__(self, fget=None, fset=None, fdel=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel

    def __set__(self, instance, value):
        self.fset(instance, value)
        return self

    def __get__(self, instance, owner):
        # 直接通过Person.data访问时instance为None, 这时也应该返回data对象本身
        if not instance:
            return self
        return self.fget(instance)

    def __delete__(self, instance):
        self.fdel(instance)
        return self

    def setter(self, fn):
        # 保留设置函数的副本
        self.fset = fn
        return self

    def deleter(self, fn):
        # 保留删除函数的副本
        self.fdel = fn
        return self


class Person:

    def __init__(self, data):
        self._data = data

    @Property  # 等价式  data=Propery(data)
    def data(self):
        return self._data

    @data.setter  # 等价式  data=data.setter(data)
    def data(self, val):
        self._data = val

    @data.deleter  # 等价式  data=data.deleter(data)
    def data(self):
        del self._data
    """
    上面三个装饰器本质就是: 
    data = Property(fget=data1, fset=data2, fdel=data3)
    """

if __name__ == '__main__':
    p = Person(19)
    p.data = 2
    print(Person.data)